﻿@model IEnumerable<Yunwei.Web.Areas.RRShop.Models.RestaurantIngredientViewModel>

@{
    ViewBag.Title = "Ingredient";
}

<script src="~/Scripts/jquery.form.js"></script>

<div class="row">
    <div class="col-lg-12">
        <h1 class="page-header">关联食材</h1>
    </div>
    <div class="col-lg-6">
        <a class="btn btn-primary btn-circle" href="/RRShop/Food/List/@ViewBag.RId"><i class="fa fa-reply"></i></a>
    </div>
</div>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Cost)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Stock)
        </th>
        <th width="200">用量</th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Cost)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Stock)/
                @Html.DisplayFor(modelItem => item.Unit)
            </td>
            <td>
                <form method="post" action="#" role="form">
                    <div class="form-group input-group">
                        <input type="hidden" name="Food_Id" value="@ViewBag.FId" />
                        <input type="hidden" name="Restaurant_Id" value="@ViewBag.RId" />
                        <input type="hidden" name="Ingredient_Id" value="@item.Ingredient_Id" />
                        <input type="text" name="Amount" class="form-control" value="@item.Amount" />
                        <span class="input-group-btn">
                            @if (item.IsFood) {
                                <input name="save" class="btn btn-danger" type="button" value="关联" />
                            }
                            else{
                                <input name="save" class="btn btn-success" type="button" value="关联"/>
                            }
                            <input name="delete" class="btn btn-warning" type="button" value="解除"/>
                        </span>
                    </div>
                </form>
            </td>
        </tr>
    }

</table>

<script src="~/Scripts/simply-toast.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("table input[name='save']").click(function () {
            var form = $(this).parent().parent().parent();
            var options = {
                url: '/RRShop/Food/AddIngredient',
                type: 'post',
                dataType: 'json',
                data: form.serialize()
            };

            $.ajax(options).done(function (resp) {
                if (resp.Success)
                    $.simplyToast('关联成功!', 'success');
                else
                    $.simplyToast('关联失败!', 'danger');
            }).error(function (a, b, c) {
                $.simplyToast('关联失败!', 'danger');
            });
        });

        $("table input[name='delete']").click(function () {
            var form = $(this).parent().parent().parent();
            var options = {
                url: '/RRShop/Food/DeleteIngredient',
                type: 'post',
                dataType: 'json',
                data: form.serialize()
            };

            $.ajax(options).done(function (resp) {
                if (resp.Success){
                    $.simplyToast('解除关联成功!', 'success');
                    window.location.reload();
                }
                else
                    $.simplyToast('解除关联失败!', 'danger');
            }).error(function (a, b, c) {
                $.simplyToast('解除关联失败!', 'danger');
            });
        });
    });
</script>